package com.egoo.ticket.server.controller;

import com.egoo.ticket.server.pojo.dto.PageOutputDto;
import com.egoo.ticket.server.pojo.dto.SelectByNameAndTypeDto;
import com.egoo.ticket.server.pojo.dto.WorkOrderTypeMenuInputDto;
import com.egoo.ticket.server.pojo.entity.WorkOrderTypeMenu;
import com.egoo.ticket.server.pojo.vo.VisibleRangeVo;
import com.egoo.ticket.server.service.WorkOrderTypeMenuService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Donny_dong
 */
@RestController
@RequestMapping(value = "/v1/workOrderTypeMenu", produces = "application/json")
@Api(value = "工单类型菜单管理",tags = "工单类型菜单管理",description = "工单类型菜单管理")
@Slf4j
public class WorkOrderTypeMenuController {
	@Autowired
	private WorkOrderTypeMenuService workOrderTypeMenuService;

	@ApiOperation(value = "根据菜单名称获取工单类型菜单信息", notes = "根据菜单名称获取工单类型菜单信息")
	@RequestMapping(method = RequestMethod.POST, value = "/selectByName")
	public DbResponse selectByName(@ApiParam(value = "selectByName") @RequestBody SelectByNameAndTypeDto selectByNameAndTypeDto,
								   HttpServletRequest request) {
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		String menuName = selectByNameAndTypeDto.getName();
		Integer currentPage = selectByNameAndTypeDto.getCurrentPage();
		Integer pageSize = selectByNameAndTypeDto.getPageSize();
		if (pageSize == null) {
			return DbResponse.getDbResponse(ResponseConstant.RES_FAIL_CODE, "分页参数不能为空！", null);
		}
		if (currentPage == null) {
			return DbResponse.getDbResponse(ResponseConstant.RES_FAIL_CODE, "分页参数不能为空！", null);
		}
		try {
			return workOrderTypeMenuService.selectByName(menuName, currentPage, pageSize,tenantId);
		}catch (Exception e){
			log.info("根据菜单名称获取工单类型菜单信息异常："+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "根据菜单名称获取工单类型菜单信息异常："+e);
		}
	}

	@ApiOperation(value = "获取所有的工单类型菜单", notes = "获取所有的工单类型菜单")
	@RequestMapping(method = RequestMethod.GET, value = "/selectAll")
	public DbResponse selectAll(@ApiParam(value = "当前页", required = true) @RequestParam("currentPage") Integer currentPage,
								@ApiParam(value = "每页行数", required = true) @RequestParam("pageSize") Integer pageSize,
								HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		PageOutputDto pageOutputDto = workOrderTypeMenuService.selectAll(currentPage, pageSize,tenantId);
		dbResponse.setRetCode(200);
		dbResponse.setData(pageOutputDto);
		return dbResponse;
	}

	@ApiOperation(value = "更新工单类型菜单的使用状态", notes = "更新工单类型菜单的使用状态")
	@RequestMapping(method = RequestMethod.GET, value = "/updateMenuUseStatus")
	public DbResponse updateMenuUseStatus(@ApiParam(value = "id", required = true) @RequestParam(name = "id") Integer id,
										  @ApiParam(value = "userId", required = true) @RequestParam(name = "userId") String userId,
										  @ApiParam(value = "Status", required = true) @RequestParam(name = "status") Integer status,
										  HttpServletRequest request) {

		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);

		userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);

		DbResponse dbResponse = new DbResponse(ResponseConstant.RES_FAIL_CODE, "更新失败");
		WorkOrderTypeMenu workOrderTypeMenu = workOrderTypeMenuService.selectById(id,tenantId);
		if (workOrderTypeMenu != null) {
			if (workOrderTypeMenu.getTagId().equals(0)) {
				int retCode = workOrderTypeMenuService.updateUseStatus(id, userId, status,tenantId);
				if (retCode == 1) {
					dbResponse.setRetCode(200);
					dbResponse.setMsg("更新成功");
				}
				return dbResponse;
			}
			if (workOrderTypeMenu.getTagId().equals(1)) {
				dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
				dbResponse.setMsg("无法更新该菜单状态");
				return dbResponse;
			}
		}

		dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
		dbResponse.setMsg("该工单类型菜单不存在");
		return dbResponse;
	}


	@ApiOperation(value = "根据id获取工单类型菜单信息", notes = "根据id获取工单类型菜单信息")
	@RequestMapping(method = RequestMethod.GET, value = "/selectById")
	public DbResponse selectById(@ApiParam(value = "id", required = true) @RequestParam(name = "id") Integer id,
								 HttpServletRequest request) {
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse();
		WorkOrderTypeMenu workOrderTypeMenu = workOrderTypeMenuService.selectById(id,tenantId);
		dbResponse.setRetCode(200);
		dbResponse.setData(workOrderTypeMenu);
		return dbResponse;
	}

	@ApiOperation(value = "新建工单类型菜单", notes = "新建工单类型菜单")
	@RequestMapping(method = RequestMethod.POST, value = "/createWorkOrderTypeMenu")
	public DbResponse createWorkOrderTypeMenu(@ApiParam(value = "workOrderTypeMenu", required = true) @RequestBody WorkOrderTypeMenuInputDto workOrderTypeMenuInputDto,
											  HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse(ResponseConstant.RES_FAIL_CODE, "新建失败");
		WorkOrderTypeMenu workOrderTypeMenu = workOrderTypeMenuService.selectByEqualsName(workOrderTypeMenuInputDto.getMenuName(),tenantId);
		if (workOrderTypeMenu != null) {
			dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
			dbResponse.setMsg("菜单名称已存在");
			return dbResponse;
		}
		String name = workOrderTypeMenuInputDto.getMenuName();
		if(StringUtils.isBlank(name) || null == name){
			dbResponse.setMsg("菜单名称不能为空");
			return dbResponse;
		}else {
			if(name.length()> ConstantCode.ControllerStrLengthCheck.LENGTH){
				dbResponse.setMsg("菜单名称不能超过80个字");
				return dbResponse;
			}
		}
		WorkOrderTypeMenu workOrderTypeMenu1 = workOrderTypeMenuService.selectByOrdering(workOrderTypeMenuInputDto.getOrdering());
		if (workOrderTypeMenu1 != null) {
			dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
			dbResponse.setMsg("状态排序已存在");
			return dbResponse;
		}
		workOrderTypeMenuInputDto.setTenantId(tenantId);
		int retCode = workOrderTypeMenuService.insertMenu(workOrderTypeMenuInputDto);
		if (retCode == 1) {
			dbResponse.setRetCode(200);
			dbResponse.setMsg("新建成功");
		}
		return dbResponse;
	}

	@ApiOperation(value = "编辑工单类型菜单", notes = "编辑工单类型菜单")
	@RequestMapping(method = RequestMethod.POST, value = "/updateContent")
	public DbResponse updateContent(@ApiParam(value = "workOrderTypeMenuInputDto", required = true) @RequestBody WorkOrderTypeMenuInputDto workOrderTypeMenuInputDto,
									HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		DbResponse dbResponse = new DbResponse(ResponseConstant.RES_FAIL_CODE, "更新失败");
		workOrderTypeMenuInputDto.setTenantId(tenantId);
		Integer id = workOrderTypeMenuInputDto.getId();
		String name = workOrderTypeMenuInputDto.getMenuName();
		WorkOrderTypeMenu workOrderTypeMenu = workOrderTypeMenuService.selectById(id,tenantId);
		//判断可见范围
		VisibleRangeVo visibleRange = workOrderTypeMenuInputDto.getVisibleRange();
		String visibleRangeType = visibleRange.getType();
		List visibleRangeValue = visibleRange.getValue();
		//判断可见范围
		if(null == visibleRangeType){
			dbResponse.setMsg("菜单可见范围不能为空");
			return dbResponse;
		}else {
			if (!visibleRangeType.equals(ConstantCode.UserRoleDeptType.ALL)){
				if (visibleRangeValue.size() == 0){
					dbResponse.setMsg("菜单可见范围不能为空");
					return dbResponse;
				}
			}
		}
		if (workOrderTypeMenu != null) {
			if(StringUtils.isBlank(name) || null == name){
				dbResponse.setMsg("菜单名称不能为空");
				return dbResponse;
			}else {
				if(name.length()> ConstantCode.ControllerStrLengthCheck.LENGTH){
					dbResponse.setMsg("菜单名称不能超过80个字");
					return dbResponse;
				}
			}
			if (workOrderTypeMenu.getMenuName().equals(name) && workOrderTypeMenu.getOrdering().equals(workOrderTypeMenuInputDto.getOrdering())) {
				workOrderTypeMenuInputDto.setMenuName(name);
				workOrderTypeMenuInputDto.setOrdering(workOrderTypeMenuInputDto.getOrdering());
				Integer retCode = workOrderTypeMenuService.updateContent(workOrderTypeMenuInputDto);
				if (retCode == 1) {
					dbResponse.setRetCode(200);
					dbResponse.setMsg("编辑成功");
				}
				return dbResponse;
			}
			if (workOrderTypeMenu.getMenuName().equals(name) && !workOrderTypeMenu.getOrdering().equals(workOrderTypeMenuInputDto.getOrdering())) {
				WorkOrderTypeMenu workOrderTypeMenu2 = workOrderTypeMenuService.selectByOrdering(workOrderTypeMenuInputDto.getOrdering());
				if (workOrderTypeMenu2 != null) {
					dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
					dbResponse.setMsg("状态排序已存在");
					return dbResponse;
				}
				workOrderTypeMenuInputDto.setMenuName(name);
				workOrderTypeMenuInputDto.setOrdering(workOrderTypeMenuInputDto.getOrdering());
				int retCode = workOrderTypeMenuService.updateContent(workOrderTypeMenuInputDto);
				if (retCode == ResponseConstant.RESULT_SUCCESS) {
					dbResponse.setRetCode(200);
					dbResponse.setMsg("编辑成功");
				}
				return dbResponse;
			}
			if (!workOrderTypeMenu.getMenuName().equals(name) && workOrderTypeMenu.getOrdering().equals(workOrderTypeMenuInputDto.getOrdering())) {
				WorkOrderTypeMenu workOrderTypeMenu1 = workOrderTypeMenuService.selectByEqualsName(name,tenantId);
				if (workOrderTypeMenu1 != null) {
					dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
					dbResponse.setMsg("菜单名称已存在");
					return dbResponse;
				}
				workOrderTypeMenuInputDto.setMenuName(name);
				workOrderTypeMenuInputDto.setOrdering(workOrderTypeMenuInputDto.getOrdering());
				int retCode = workOrderTypeMenuService.updateContent(workOrderTypeMenuInputDto);
				if (retCode == ResponseConstant.RESULT_SUCCESS) {
					dbResponse.setRetCode(200);
					dbResponse.setMsg("编辑成功");
				}
				return dbResponse;

			}
			if (!workOrderTypeMenu.getMenuName().equals(name) && !workOrderTypeMenu.getOrdering().equals(workOrderTypeMenuInputDto.getOrdering())) {
				WorkOrderTypeMenu workOrderTypeMenu1 = workOrderTypeMenuService.selectByEqualsName(name,tenantId);
				if (workOrderTypeMenu1 != null) {
					dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
					dbResponse.setMsg("菜单名称已存在");
					return dbResponse;
				}
				WorkOrderTypeMenu workOrderTypeMenu2 = workOrderTypeMenuService.selectByOrdering(workOrderTypeMenuInputDto.getOrdering());
				if (workOrderTypeMenu2 != null) {
					dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
					dbResponse.setMsg("状态排序已存在");
					return dbResponse;
				}
				workOrderTypeMenuInputDto.setMenuName(name);
				workOrderTypeMenuInputDto.setOrdering(workOrderTypeMenuInputDto.getOrdering());
				int retCode = workOrderTypeMenuService.updateContent(workOrderTypeMenuInputDto);
				if (retCode == ResponseConstant.RESULT_SUCCESS) {
					dbResponse.setRetCode(200);
					dbResponse.setMsg("编辑成功");
				}
				return dbResponse;
			}
		}

		dbResponse.setRetCode(ResponseConstant.RES_FAIL_CODE);
		dbResponse.setMsg("没有该工单状态");
		return dbResponse;

	}

	@ApiOperation(value = "提供默认状态排序值", notes = "提供默认状态排序值")
	@RequestMapping(value = "/selectOrder", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public DbResponse selectOrder() {
		DbResponse dbResponse = new DbResponse();
		int retCode = workOrderTypeMenuService.selectOrderMax();
		dbResponse.setRetCode(200);
		dbResponse.setData(retCode);
		return dbResponse;
	}

	@ApiOperation(value = "根据userId获取工单类型菜单信息", notes = "根据userId获取工单类型菜单信息")
	@RequestMapping(method = RequestMethod.GET, value = "/selectByUserId")
	public DbResponse selectByUserId(@ApiParam(value = "id", required = true) @RequestParam(name = "id") String id,
									 HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		log.info("获取类型菜单header传过来的用户id为："+userId);
		log.info("获取类型菜单header传过来的租户id为："+tenantId);
		try {
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.SELECT_SUCCESS_MSG,
					workOrderTypeMenuService.getTypeMenuAndFilterMenuListByUserId(id,tenantId));
		} catch (Exception e) {
			log.error("根据userId获取工单类型菜单信息异常："+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"根据userId" +
					"获取工单类型菜单信息异常："+e);
		}
	}

}
